<?php

namespace zhtfrontend\modules\parentsgrowth\controllers;

use yii\db\Query;
use zhtfrontend\models\datas\Admin;
use zhtfrontend\models\datas\CommentRelation;
use zhtfrontend\models\datas\Comments;
use zhtfrontend\models\datas\Mechanic;
use zhtfrontend\modules\teachergrowth\components\rest\ActiveController;
use common\components\helpers\Fun;
use common\models\datas\Student;
use doumiaoduo\aliyun\OssuploaderHelper;
use yii\console\Exception;
use yii\data\ActiveDataProvider;
use yii\web\UploadedFile;
use zhtfrontend\models\datas\ClassLesson;
use zhtfrontend\models\datas\ClassManage;
use zhtfrontend\models\datas\ClassManageAttribute;
use zhtfrontend\models\datas\ClassManageTeacher;
use zhtfrontend\models\datas\ClassManageTime;
use zhtfrontend\models\datas\ClassStudent;
use zhtfrontend\models\datas\LessonMedia;
use zhtfrontend\models\datas\LessonStudents;
use zhtfrontend\models\datas\Medias;
use zhtfrontend\models\datas\StudentCheck;
use zhtfrontend\models\datas\Teacher;
use yii\helpers\ArrayHelper;
use yii\web\ForbiddenHttpException;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;
use yii\web\Response;

class GrowthdiaryController extends ActiveController

{
    private $_config;
    public $modelClass = 'zhtfrontend\models\datas\ClassManage';

    public $serializer = [
        'class' => 'zhtfrontend\modules\parentsgrowth\components\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        return $behaviors;
    }

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['create'], $actions['update'], $actions['delete'], $actions['view']);
        return $actions;
    }



    public function actionCouselist(){
        $data = \Yii::$app->request->post();
        $student_id=ArrayHelper::keyExists('student_id', $data) ? $data['student_id'] : '';
        $year = ArrayHelper::keyExists('year', $data) ? $data['year'] : '';
        $month = ArrayHelper::keyExists('month', $data) ? $data['month'] : '';
        $day = ArrayHelper::keyExists('day', $data) ? $data['day'] : '';
        if (empty($student_id)){
            return ['statusCode' => 0, 'message' => 'error', 'result' => '当前学生不存在'];
        }else{
            if (!empty($year) && !empty($month) && empty($day)){ //只有年月
                $time_start=strtotime($year."-".$month."-".'01' ."00:00:00");
                $time_end=strtotime($year."-".$month."-".'31' ."23:59:59");
            }elseif (!empty($year)&& empty($month) && empty($day)){//只有年
                $time_start=strtotime($year."-".'01'."-".'01' ."00:00:00");
                $time_end=strtotime($year."-".'12'."-".'31' ."23:59:59");
            }else{//年月日都有
                $time_start=strtotime($year."-".$month."-".$day."00:00:00");
                $time_end=strtotime($year."-".$month."-".$day."23:59:59");
            }
            //下面就是查找class_manage的相关的信息
            $class_id=(new Query())->select('class_manage_id')->from('{{%class_student}}')->where(['student_id' => $student_id])->column(Fun::getSiteDb());
            $class_manage_query=(new Query())->select('*')
                ->from('{{%class_manage}} cm')
                ->leftJoin('{{%class_lesson}}  cl','cl.class_id=cm.class_id')
                ->where(['cm.class_id' => $class_id,'cm.status'=>[1,2]]);
            if (isset($time_start) && !empty($time_start)){
                $class_manage_query=$class_manage_query->andWhere(['between','cl.class_date',$time_start,$time_end]);
            }

            $current_class_manage=$class_manage_query->all(Fun::getSiteDb());


            if(empty($current_class_manage)){
                return ['statusCode' => 202, 'message' => 'error', 'result' => '暂时没有课程'];
            }else{
                $course=[];
                foreach ($current_class_manage as $ks=>$vs){
                    switch ($vs['status']){
                        case 0:
                            $vs['status']='未开班';break;
                        case 1:
                            $vs['status']='已开班';break;
                        case 2:
                            $vs['status']='已结束';break;
                    };
                    $class_manage_attribute=ClassManageAttribute::find()->where(['class_id'=>$vs['class_id']])->one();
                    $class_manage_time=ClassManageTime::find()->where(['class_id'=>$vs['class_id']])->one();
                    //需要查找一下当前这个课程的老师的图片
                    $class_manage_teacher=(new Query())->select('teacher_id')->from('{{%class_manage_teacher}}')->where(['class_id'=>$vs['class_id']])->column(Fun::getSiteDb());
                    $teacher_info=(new Query())->select('img,name')->from('{{%teacher}}')->where(['id'=>$class_manage_teacher])->one(Fun::getSiteDb());
                    $course[$vs['class_id']]=[
                        'id'=>$vs['class_id'],
                        'lesson_id'=>isset($vs["lesson_id"])?$vs["lesson_id"]:'',
                        'teacher_img'=>(isset($teacher_info['img'])&& !empty($teacher_info['img']))?$teacher_info['img']:'../../images/teach-log-img-no-have.jpg',
                        'teacher_name'=>(isset($teacher_info['name'])&& !empty($teacher_info['name']))?$teacher_info['name']:'',
                        'course_name'=>isset($class_manage_attribute->course)?$class_manage_attribute->course:'',
                        'course_count'=>isset($vs["lesson"])?$vs["lesson"]:'', //当前是第几节课
                        'class_time_day'=>isset($class_manage_time->class_day)?$class_manage_time->class_day:'',
                        'class_time_start'=>isset($class_manage_time->start_time)?$class_manage_time->start_time:'',
                        'class_time_end'=>isset($class_manage_time->end_time)?$class_manage_time->end_time:'',
                        'status'=>$vs['status'],
                    ];
                }
                $items=[
                    'course_info'=>$course,
                    'course_counts'=>count($course)
                ];

                return ['statusCode' => 200, 'message' => 'success', 'items' => $items];
            }
        }
    }

    public function actionCousedetail(){
        $data = \Yii::$app->request->post();
        $class_id=ArrayHelper::keyExists('class_id', $data) ? $data['class_id'] : '';
        $lesson_id=ArrayHelper::keyExists('lesson_id', $data) ? $data['lesson_id'] : '';
        $student_id=ArrayHelper::keyExists('student_id', $data) ? $data['student_id'] : '';
        if (empty($class_id) || empty($lesson_id) || empty($student_id)){
            return ['statusCode' => 0, 'message' => 'error', 'result' => '信息不存在'];
        }else{
            $items=[];
            $class_info=(new Query())->from('{{%class_manage_attribute}}')->select('course')->where(['class_id'=>$class_id])->one(Fun::getSiteDb());
            $lesson_info=(new  Query())->from('{{%class_lesson}}')->where(['class_id'=>$class_id,'lesson_id'=>$lesson_id])->one(Fun::getSiteDb());
            $class_manage_teacher=(new Query())->select('teacher_id')->from('{{%class_manage_teacher}}')->where(['class_id'=>$class_id])->column(Fun::getSiteDb());
            $teacher_info=(new Query())->select('id,img,name')->from('{{%teacher}}')->where(['id'=>$class_manage_teacher])->one(Fun::getSiteDb());
            $lesson_students=(new Query())->select('*')->from('{{%lesson_students}}')->where(['lesson_id'=>$lesson_id,'student_id'=>$student_id,'status'=>1])->one(Fun::getSiteDb());

            if (empty($lesson_students)){
                return ['statusCode' => 201, 'message' => 'error', 'result' => '未生成签到'];
            }
            //下面是需要查找老师提交的照片的相关的信息 0是上课花絮
            $lesson_media_image=LessonMedia::find()->where(['lesson_id'=>$lesson_id,'student_id'=>$student_id,'is_refouce'=>0,'m_type'=>0])->one();
            $medias_images_array=[];
            if (isset($lesson_media_image)){
                $medias_images=Medias::find()->where(['object_id'=>$lesson_media_image->id])->orderBy('id DESC')->limit(4)->all();
                foreach ($medias_images as $key=>$val){
                    $medias_images_array[$key]=$val->file_path;
                }
            }
            //下面是查找老师提交的视频的相关信息 0是上课花絮
            $lesson_media_video=LessonMedia::find()->where(['lesson_id'=>$lesson_id,'student_id'=>$student_id,'is_refouce'=>0,'m_type'=>1])->one();
            $medias_video_src='';
            if (isset($lesson_media_video)){
                $medias_video=Medias::find()->where(['object_id'=>$lesson_media_video->id])->orderBy('id DESC')->one();
                $medias_video_src=$medias_video->file_path;
            }
            switch ($lesson_students['t_score']){
                case 1:
                    $lesson_students['t_score']='无比出色';
                    $t_score_img='../../images/evaluate1.jpg';break;
                case 2:
                    $lesson_students['t_score']='成长进步';
                    $t_score_img='../../images/evaluate2.jpg';break;
                case 3:
                    $lesson_students['t_score']='表现出众';
                    $t_score_img='../../images/evaluate3.jpg';break;
                case 4:
                    $lesson_students['t_score']='创意闪闪';
                    $t_score_img='../../images/evaluate4.jpg';break;
                default:
                    $lesson_students['t_score']='暂时未评价';
                    $t_score_img='../../images/evaluateno.png';break;
            }
            $lesson_students_info=[
                'id'=>isset($lesson_students['id'])?$lesson_students['id']:'',
                't_score'=>isset($lesson_students['t_score'])?$lesson_students['t_score']:'',
                't_score_img'=>$t_score_img,
                't_content_text'=>isset($lesson_students['t_content_text'])?$lesson_students['t_content_text']:'',
                't_fource_text'=>isset($lesson_students['t_fource_text'])?$lesson_students['t_fource_text']:'',
                's_question_text'=>isset($lesson_students['s_question_text'])?$lesson_students['s_question_text']:'',
                't_answer_text'=>isset($lesson_students['t_answer_text'])?$lesson_students['t_answer_text']:'',
                'medias_images_array'=>isset($medias_images_array)?$medias_images_array:[],
                'medias_video_src'=>isset($medias_video_src)?$medias_video_src:'',
            ];
            //下面查找学生信息
            $student_info=(new  Query())->from('{{%student}}')->where(['id'=>$student_id])->one();
            //下面查找一下有没有上节课的信息
            $last_class=['status'=>true];
            if (!isset($lesson_info['lesson']) ||  $lesson_info['lesson']<=1){
                $last_class=[
                    'status'=>true
                ];
            }else{
                $last_lesson=$lesson_info['lesson']-1;
                $last_lesson_info=(new  Query())->from('{{%class_lesson}}')->where(['class_id'=>$class_id,'lesson'=>$last_lesson])->one(Fun::getSiteDb());
                if (isset($last_lesson_info) && !empty($last_lesson_info)){
                    $last_lesson_students=(new Query())->select('*')->from('{{%lesson_students}}')->where(['lesson_id'=>$last_lesson_info['lesson_id'],'student_id'=>$student_id,'status'=>1])->one(Fun::getSiteDb());
                    if (isset($last_lesson_students) && !empty($last_lesson_students)){
                        $last_class=[
                            'lesson_id'=>$last_lesson_info['lesson_id'],
                            'lesson'=>$last_lesson_info['lesson'],
                            'status'=>false
                        ];
                    }
                }else{
                    $last_class=[
                        'status'=>true
                    ];
                }
            }
            //下面查看有没有下节课的信息
            $next_class=[
                'status'=>true
            ];
            if (!isset($lesson_info['lesson'])){
                $next_class=[
                    'status'=>true
                ];
            }else{
                $next_lesson=$lesson_info['lesson']+1;
                $next_lesson_info=(new  Query())->from('{{%class_lesson}}')->where(['class_id'=>$class_id,'lesson'=>$next_lesson])->one(Fun::getSiteDb());
                if (isset($next_lesson_info) && !empty($next_lesson_info)){
                    $next_lesson_students=(new Query())->select('*')->from('{{%lesson_students}}')->where(['lesson_id'=>$next_lesson_info['lesson_id'],'student_id'=>$student_id,'status'=>1])->one(Fun::getSiteDb());
                    if (isset($next_lesson_students) && !empty($next_lesson_students)){
                        $next_class=[
                            'lesson_id'=>$next_lesson_info['lesson_id'],
                            'lesson'=>$next_lesson_info['lesson'],
                            'status'=>false
                        ];
                    }
                }else{
                    $next_class=[
                        'status'=>true
                    ];
                }
            }

            //下面是查看有没有对教师进行评价
            $comments=(new Query())->select('*')->from('{{%comments}}')->where(['valuator_type'=>1,'valuator_id'=>$student_id,'accepter_type'=>2,'accepter_id'=>$teacher_info['id'],'lesson_student_id'=>$lesson_students_info['id'],'status'=>1])->one(Fun::getSiteDb());
            if (isset($comments) && !empty($comments)){
                $comments_status=false;
            }else{
                $comments_status=true;
            }

            $items=[
                'class_info'=>isset($class_info['course'])?$class_info['course']:'',
                'lesson_info'=>isset($lesson_info['lesson'])?$lesson_info['lesson']:'',
                'student_info'=>$student_info,
                'teacher_info'=>[
                    'id'=>isset($teacher_info['id'])?$teacher_info['id']:'',
                    'name'=>isset($teacher_info['name'])?$teacher_info['name']:'',
                    'img'=>(isset($teacher_info['img'])&&!empty($teacher_info['img']))?$teacher_info['img']:'../../images/teach-log-img-no-have.jpg',
                ],
                'lesson_students_info'=>$lesson_students_info,
                'last_class'=>$last_class,
                'next_class'=>$next_class,
                'comments_status'=>$comments_status
            ];

            return ['statusCode' => 200, 'message' => 'error', 'items' => $items];
        }
    }

    //下面是查看上节课作品反馈
    public function actionPrevfeedback(){
        $data = \Yii::$app->request->post();
        $lesson_id=ArrayHelper::keyExists('lesson_id', $data) ? $data['lesson_id'] : '';
        $student_id=ArrayHelper::keyExists('student_id', $data) ? $data['student_id'] : '';
        $lesson_students=(new Query())->select('*')->from('{{%lesson_students}}')->where(['lesson_id'=>$lesson_id,'student_id'=>$student_id,'status'=>1])->one(Fun::getSiteDb());
        if (empty($lesson_students)){
            return ['statusCode' => 201, 'message' => 'error', 'result' => '未生成签到'];
        }else{
            //下面需要查找学生上节课反馈的作品 0为上课花絮 1是课下练习作品
            $lesson_media_image_back=LessonMedia::find()->where(['lesson_id'=>$lesson_id,'student_id'=>$student_id,'is_refouce'=>1,'m_type'=>0])->one();
            $medias_images_array_back=[];
            if (isset($lesson_media_image_back)){
                $medias_images=Medias::find()->where(['object_id'=>$lesson_media_image_back->id])->orderBy('id DESC')->limit(4)->all();
                foreach ($medias_images as $key=>$val){
                    $medias_images_array_back[$key]=$val->file_path;
                }
            }
            //下面是查找老师提交的视频的相关信息 0是上课花絮
            $lesson_media_video_back=LessonMedia::find()->where(['lesson_id'=>$lesson_id,'student_id'=>$student_id,'is_refouce'=>0,'m_type'=>1])->one();
            $medias_video_src_back='';
            if (isset($lesson_media_video_back)){
                $medias_video=Medias::find()->where(['object_id'=>$lesson_media_video_back->id])->orderBy('id DESC')->one();
                $medias_video_src_back=$medias_video->file_path;
            }
            //下面是学生的提问信息 上节课的
            $t_feedback_score=isset($lesson_students['t_feedback_score'])?$lesson_students['t_feedback_score']:'';
            $s_question_text=isset($lesson_students['s_question_text'])?$lesson_students['s_question_text']:'';
            $t_answer_text=isset($lesson_students['t_answer_text'])?$lesson_students['t_answer_text']:'';
            switch ($t_feedback_score){
                case 1:
                    $t_feedback_score_text='无比出色';
                    $t_score_img='../../images/evaluate1.jpg';break;
                case 2:
                    $t_feedback_score_text='成长进步';
                    $t_score_img='../../images/evaluate2.jpg';break;
                case 3:
                    $t_feedback_score_text='表现出众';
                    $t_score_img='../../images/evaluate3.jpg';break;
                case 4:
                    $t_feedback_score_text='创意闪闪';
                    $t_score_img='../../images/evaluate4.jpg';break;
                default:
                    $t_feedback_score_text='暂时未评价';
                    $t_score_img='../../images/evaluateno.png';break;
            }
            $items=[
                'feedback_info'=>[
                    'medias_images_array_back'=>$medias_images_array_back,
                    'medias_video_src_back'=>$medias_video_src_back,
                    't_feedback_score'=>$t_feedback_score_text,
                    't_score_img'=>$t_score_img,
                    's_question_text'=>$s_question_text,
                    't_answer_text'=>$t_answer_text,
                ]
            ];
            return ['statusCode' => 200, 'message' => 'error', 'items' => $items];
        }
    }


    //这个地方主要是家长（学生评价）老师和教辅总监
    public function actionEvaluate(){
        $data = \Yii::$app->request->post();
        $lesson_students_id=ArrayHelper::keyExists('lesson_students_id', $data) ? $data['lesson_students_id'] : '';
        $student_id=ArrayHelper::keyExists('student_id', $data) ? $data['student_id'] : '';
        $teacher_id=ArrayHelper::keyExists('teacher_id', $data) ? $data['teacher_id'] : '';
        if (empty($lesson_students_id) || empty($student_id) || empty($teacher_id)){
            return ['statusCode' => 0, 'message' => 'error', 'result' => '信息不存在'];
        }else{
            $teacher_info=(new Query())->select('*')->from('{{%teacher}}')->where(['id'=>$teacher_id])->one(Fun::getSiteDb());
            $mechanic_info=Mechanic::findOne($teacher_info['mechanic_id']);
            $admin_info=Admin::find()->where(['id'=>$mechanic_info->admin_id])->one();
            //下面是查找评价的相关信息,首先是查找评价条目,教师的和教辅总监的;
            $comments_info_teacher=(new Query())->select("*")->from('{{%comments_info}}')->where(['valuator_type'=>1,'accepter_type'=>2,'status'=>1])->all(Fun::getSiteDb());
            $comments_info_director=(new  Query())->select("*")->from('{{%comments_info}}')->where(['valuator_type'=>1,'accepter_type'=>3,'status'=>1])->all(Fun::getSiteDb());
            //下面是查找教师的
            $comments_info_teacher_arr=[];
            $comments_info_director_arr=[];
            foreach ($comments_info_teacher as $key=>$val){
                $evaluateArray=[];
                $score=1;
                //需要查看是否评分
                $comments=(new Query())->select('*')->from('{{%comments}}')->where(['valuator_type'=>1,'valuator_id'=>$student_id,'accepter_type'=>2,'accepter_id'=>$teacher_id,'lesson_student_id'=>$lesson_students_id,'status'=>1])->one(Fun::getSiteDb());
                if (isset($comments) && !empty($comments)){
                    $comment_relation=(new Query())->select('*')->from('{{%comment_relation}}')->where(['comment_id'=>$comments['id'],'comment_info_id'=>$val['id']])->one(Fun::getSiteDb());
                    if (isset($comment_relation) && !empty($comment_relation)){
                        $score=(int)$comment_relation['for_value'];
                    }
                }
                $evaluateArrays=$this->generateScore($score);
                $comments_info_items[$key]=[
                    'id'=>$val['id'],//这个地方的id是comments_info的id
                    'title'=>$val['title'],
                    'description'=>$val['description'],
                    'currentItem'=>$score,
                    'comment_info_value'=>$evaluateArrays,
                ];
                $comments_info_teacher_arr=$comments_info_items;
            }

            //下面是查找教辅总监的
            foreach ($comments_info_director as $keys=>$vals){
                $evaluateArraysd=[];
                $scored=1;
                //需要查看是否评分
                $commentsd=(new Query())->select('*')->from('{{%comments}}')->where(['valuator_type'=>1,'valuator_id'=>$student_id,'accepter_type'=>3,'accepter_id'=>$admin_info['id'],'lesson_student_id'=>$lesson_students_id,'status'=>1])->one(Fun::getSiteDb());
                if (isset($commentsd) && !empty($commentsd)){
                    $comment_relationd=(new Query())->select('*')->from('{{%comment_relation}}')->where(['comment_id'=>$commentsd['id'],'comment_info_id'=>$vals['id']])->one(Fun::getSiteDb());
                    if (isset($comment_relationd) && !empty($comment_relationd)){
                        $scored=(int)$comment_relationd['for_value'];
                    }
                }
                $evaluateArraysd=$this->generateScore($scored);
                $comments_info_itemsd[$keys]=[
                    'id'=>$vals['id'],//这个地方的id
                    'title'=>$vals['title'],
                    'description'=>$vals['description'],
                    'currentItem'=>$scored,
                    'comment_info_value'=>$evaluateArraysd,
                ];
                $comments_info_director_arr=$comments_info_itemsd;
            }

            $items=[
                'teacher_info'=>[
                    'name'=>isset($teacher_info['name'])?$teacher_info['name']:'',
                    'img'=>(isset($teacher_info['img']) && !empty($teacher_info['img']))?$teacher_info['img']:'../../images/teach-log-img-no-have.jpg',
                    'sex_arr'=>$this->generateSex($teacher_info['sex'])

                ],
                'director_info'=>[
                    'name'=>(isset($admin_info['name']) && !empty($admin_info['name']))?$admin_info['name']:'',
                    'img'=>(isset($admin_info['logo']) && !empty($admin_info['logo']))?$admin_info['logo']:'../../images/teach-log-img-no-have.jpg',
                    'sex_arr'=>$this->generateSex(0),
                 ],
                'comments_info_teacher_arr'=>$comments_info_teacher_arr,
                'comments_info_director_arr'=>$comments_info_director_arr,
            ];
            return ['statusCode' => 200, 'message' => 'error', 'items' => $items];
        }
    }

    public function actionEvaluatesubmit(){
        $data = \Yii::$app->request->post();
        $lesson_students_id=ArrayHelper::keyExists('lesson_students_id', $data) ? $data['lesson_students_id'] : '';
        $student_id=ArrayHelper::keyExists('student_id', $data) ? $data['student_id'] : '';
        $teacher_id=ArrayHelper::keyExists('teacher_id', $data) ? $data['teacher_id'] : '';
        $comments_teacher_value=ArrayHelper::keyExists('comments_teacher_value', $data) ? $data['comments_teacher_value'] : '';
        $comments_director_value=ArrayHelper::keyExists('comments_director_value', $data) ? $data['comments_director_value'] : '';
        $evaluate_info=[
            'student_id'=>$student_id,
            'teacher_id'=>$teacher_id,
            'lesson_students_id'=>$lesson_students_id,
        ];

        //首先查找家长对老师的评价有没有valuator_type(评价者类型是1被评价者类型是2)
        $comments_teacher_results=$this->submitEvaluate($evaluate_info,2,$comments_teacher_value);
        $comments_director_results=$this->submitEvaluate($evaluate_info,3,$comments_director_value);
        $comments_results=[
            'comments_teacher_results'=>count($comments_teacher_results),
            'comments_director_results'=>count($comments_director_results),
        ];
        return ['statusCode' => 200, 'message' => 'error', 'items' => $comments_results];
    }


    public function submitEvaluate($evaluate_info,$accepter_type,$comments_value){
        //$evaluate_info=['student_id'='','teacher_id'='','lesson_students_id'='']
        $title='';
        $accepter_id='';
        $teacher_info=(new Query())->select('*')->from('{{%teacher}}')->where(['id'=>$evaluate_info['teacher_id']])->one(Fun::getSiteDb());
        $mechanic_info=Mechanic::findOne($teacher_info['mechanic_id']);
        $admin_info=Admin::find()->where(['id'=>$mechanic_info->admin_id])->one();
        if ($accepter_type==2){
            $title='学生对教师的评价';
            $accepter_id=$evaluate_info['teacher_id'];
        }elseif ($accepter_type==3){
            $title='学生对教辅总监的评价';
            $accepter_id=$admin_info['id'];
        }
        $comments=(new Query())->select('*')->from('{{%comments}}')->where(['valuator_type'=>1,'valuator_id'=>$evaluate_info['student_id'],'accepter_type'=>$accepter_type,'accepter_id'=>$accepter_id,'lesson_student_id'=>$evaluate_info['lesson_students_id'],'status'=>1])->one(Fun::getSiteDb());
        if (isset($comments) && !empty($comments)){
            $comments_id=$comments['id'];
        }else{
            $new_comments=new Comments();
            $new_comments->title=$title;
            $new_comments->description=$title;
            $new_comments->user_id=$evaluate_info['student_id'];
            $new_comments->valuator_type=1;
            $new_comments->valuator_id=$evaluate_info['student_id'];
            $new_comments->accepter_type=$accepter_type;
            $new_comments->accepter_id=$accepter_id;
            $new_comments->lesson_student_id=$evaluate_info['lesson_students_id'];;
            $new_comments->status=1;
            $new_comments->created_at=time();
            $new_comments->updated_at=time();
            if ($new_comments->save(false)){
                $comments_id=$new_comments->id;
            }else{
                return ['statusCode' => 0, 'message' => 'error', 'result' => '保存失败'];
            }
        }

        foreach ($comments_value as $key=>$val){

            $comment_relation=CommentRelation::find()->where(['comment_id'=>$comments_id,'comment_info_id'=>$val['id']])->one();
            if (isset($comment_relation) && !empty($comment_relation)){
                $comment_relation->for_value=$val['value'];
                $comment_relation->save(false);
            }else{
                $new_comment_relation=new CommentRelation();
                $new_comment_relation->comment_id=(int)$comments_id;
                $new_comment_relation->comment_info_id=(int)$val['id'];
                $new_comment_relation->for_value=$val['value'];
                $new_comment_relation->save(false);
            }
        }
        return $comments_value;
    }

    public function generateScore($score){
        $icon='';
        $current_item='';
        $evaluateArray=[];
        for ($i=1; $i<=4; $i++){
            switch ($i){
                case 1:
                    $icon='icon-sun';break;
                case 2:
                    $icon='icon-bean';break;
                case 3:
                    $icon='icon-seeding';break;
                case 4:
                    $icon='icon-flower';break;
            }
            $evaluateArray[$i]=[
                'id'=>$i,
                'icon'=>$icon,
                'evaluateCon'=>$i.'分',
                'active'=>'',
            ];
            switch ($score){
                case 1:
                    $evaluateArray[1]['active']='dmd-active';break;
                case 2:
                    $evaluateArray[2]['active']='dmd-active';break;
                case 3:
                    $evaluateArray[3]['active']='dmd-active';break;
                case 4:
                    $evaluateArray[4]['active']='dmd-active';break;
            }
        }
        return $evaluateArray;
    }

    //下面是性别
    public function generateSex($sex){
        $sex_arr=[
            'sexIcon'=>'icon-male',
            'sexBox'=>'dmd-female-icon',
        ];
        switch ($sex){
            case 0:
                $sex_arr=[
                    'sexIcon'=>'icon-female',
                    'sexBox'=>'dmd-female-icon',
                ];break;
        }
        return $sex_arr;

    }

    //课下练习反馈的功能 上传之前需要把之前上传过的查找出来
    public function actionPractice(){//查找课下练习反馈
        $data = \Yii::$app->request->post();
        $student_id=ArrayHelper::keyExists('student_id', $data) ? $data['student_id'] : '';
        $lesson_id=ArrayHelper::keyExists('lesson_id', $data) ? $data['lesson_id'] : '';
        //查找lesson_media表里面的图片数据
        $lesson_media_image=LessonMedia::find()->where(['lesson_id'=>$lesson_id,'student_id'=>$student_id,'m_type'=>0,'is_refouce'=>1])->one();
        $medias_images_array=[];
        if (isset($lesson_media_image)){
            $medias_images=Medias::find()->where(['object_id'=>$lesson_media_image->id])->orderBy('id DESC')->limit(4)->all();
            foreach ($medias_images as $key=>$val){
                $medias_images_array[$key]=$val->file_path;
            }
        }

        $lesson_media_video=LessonMedia::find()->where(['lesson_id'=>$lesson_id,'student_id'=>$student_id,'m_type'=>1,'is_refouce'=>1])->one();
        $medias_video_src='';
        if (isset($lesson_media_video)){
            $medias_video=Medias::find()->where(['object_id'=>$lesson_media_video->id])->orderBy('id DESC')->one();
            $medias_video_src=$medias_video->file_path;
        }
        //查找问题反馈的数据
        $lesson_students=LessonStudents::find()->where(['lesson_id'=>$lesson_id,'student_id'=>$student_id])->one();
        $question=[
            'question_text'=>$lesson_students->s_question_text,
            'question_voice'=>$lesson_students->s_question_voice,
            'second'=>$lesson_students->s_question_second,
        ];
        $medias_info=[
            'image'=>$medias_images_array,
            'video'=>$medias_video_src,
            'question'=>$question
        ];
        return ['statusCode' => 200, 'message' => 'error', 'items' => $medias_info];
    }


    //下面是提交课下练习反馈的功能(保存)
    public function actionAfterfeedback(){
        $data = \Yii::$app->request->post();
        $student_id=ArrayHelper::keyExists('student_id', $data) ? $data['student_id'] : '';
        $lesson_id=ArrayHelper::keyExists('lesson_id', $data) ? $data['lesson_id'] : '';
        $image_array=ArrayHelper::keyExists('img_array', $data) ? $data['img_array'] : '';
        $video=ArrayHelper::keyExists('video_src', $data) ? $data['video_src'] : '';
        $question_text=ArrayHelper::keyExists('question_text', $data) ? $data['question_text'] : '';
        //首先保存一下课下问题反馈的信息吧
        $lesson_students=LessonStudents::find()->where(['lesson_id'=>$lesson_id,'student_id'=>$student_id])->one();
        if (!empty($lesson_students)){
            $lesson_students->s_question_text=$question_text;
            $lesson_students->save(false);
        }
        //首先是图片,这个地方的is_refouce说明是课下练习反馈也就是是学生提交的自己的练习作品 'is_refouce'=>1说明是课下练习反馈
        $lesson_media_image=LessonMedia::find()->where(['lesson_id'=>$lesson_id,'student_id'=>$student_id,'m_type'=>0,'is_refouce'=>1])->one();
        if (!empty($lesson_media_image)){
            $lesson_mediai=$lesson_media_image;
        }else{
            $lesson_mediai=new LessonMedia();
        }
        //下面是保存到dmd_lesson_media表里面去,首先是图片
        if(!empty($image_array)){
            $lesson_mediai->lesson_id=isset($lesson_id)?$lesson_id:'';
            $lesson_mediai->student_id=isset($student_id)?$student_id:'';
            $lesson_mediai->m_type=0;
            $lesson_mediai->is_refouce=1;
            $lesson_mediai->status=1;
            if ($lesson_mediai->save()){
                $lesson_media_id=$lesson_mediai->id;
                //下面是保存到dmd_medias表里面去
                foreach ($image_array as $key=>$val){
                    $medias=new Medias();
                    $medias->class_name='zhtfrontend\models\datas\LessonMedia';
                    $medias->object_id=$lesson_media_id;
                    $medias->file_path=$val;
                    $medias->alt='';//现在好像没有描述
                    $medias->created_at=time();
                    $medias->updated_at=time();
                    $medias->is_default=1;
                    $medias->type=1; //1是图片
                    $medias->save();
                }
            }
        }
        //下面是视频的上传
        $lesson_media_video=LessonMedia::find()->where(['lesson_id'=>$lesson_id,'student_id'=>$student_id,'m_type'=>1,'is_refouce'=>1])->one();
        if (!empty($lesson_media_video)){
            $lesson_mediasv=$lesson_media_video;
        }else{
            $lesson_mediasv=new LessonMedia();
        }

        if (!empty($video)){
            $lesson_mediasv->lesson_id=isset($lesson_id)?$lesson_id:'';
            $lesson_mediasv->student_id=isset($student_id)?$student_id:'';
            $lesson_mediasv->m_type=1;//0是图片 1是视频
            $lesson_mediasv->is_refouce=1;//0是教师上传的花絮 1家长反馈的上课练习
            $lesson_mediasv->status=1;
            if ($lesson_mediasv->save()){
                $lesson_media_id=$lesson_mediasv->id;
                //下面是保存到dmd_medias表里面去，视频就一条记录，就不用循环了
                $medias=new Medias();
                $medias->class_name='zhtfrontend\models\datas\LessonMedia';
                $medias->object_id=$lesson_media_id;
                $medias->file_path=$video;
                $medias->alt='';//现在好像没有描述
                $medias->created_at=time();
                $medias->updated_at=time();
                $medias->is_default=1;
                $medias->type=2; //1是图片 2是视频
                $medias->save();
            }
        }
        $items=[
            'image'=>$lesson_mediai,
            'video'=>$lesson_mediasv,
        ];
        return ['statusCode' => 200, 'message' => 'error', 'items' => $items];
    }



    //上传
    public function actionUpload(){
        $this->_config=\Yii::$app->params['aliyunoss'];
        parent::init();
        $config = array(
            "pathFormat" =>$this->_config['filePathFormat'],//filePathFormat为"/uploadfiles/web/site/file/{yyyy}{mm}{dd}/{time}{rand:6}"
            "maxSize" => $this->_config['fileMaxSize'],
            "allowFiles" => $this->_config['fileAllowFiles']
        );
        $fieldName = 'file';
        $up=new OssuploaderHelper($fieldName,$config);
        return $up->getOssFileInfo();
    }

}